/* Copyright © 2010 LPRM
Universidade Federal do Espírito Santo - RNP-GT CWTools

This file is part of JoinUs!.

JoinUs! is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

JoinUs! is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with JoinUs!. If not, see <http://www.gnu.org/licenses/>. */
package br.ufes.cwtools.joinus.profile_manager.profile;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Contact extends Profile {

	public String priEmail="none", secEmail1="", secEmail2="", secEmail3="";

	public String IM1user="", IM2user="", homePhone="", cellphone="", addr1="", addr2="", city="",
			state="", zipCode="";
	public int IM1Server, IM2Server;

	public String toXML() {
		StringBuilder output = new StringBuilder();

		output.append("<?xml version=\"1.0\"+encoding=\"utf-8\"?><resources>");
		output.append("<item name=\"IM1Server\">" + IM1Server + "</item>");
		output.append("<item name=\"IM2Server\">" + IM2Server + "</item>");
		output.append("<item name=\"priEmail\">" + priEmail + "</item>");
		output.append("<item name=\"secEmail1\">" + secEmail1 + "</item>");
		output.append("<item name=\"secEmail2\">" + secEmail2 + "</item>");
		output.append("<item name=\"secEmail3\">" + secEmail3 + "</item>");
		output.append("<item name=\"IM1user\">" + IM1user + "</item>");
		output.append("<item name=\"IM2user\">" + IM2user + "</item>");
		output.append("<item name=\"homePhone\">" + homePhone + "</item>");
		output.append("<item name=\"cellphone\">" + cellphone + "</item>");
		output.append("<item name=\"addr1\">" + addr1 + "</item>");
		output.append("<item name=\"addr2\">" + addr2 + "</item>");
		output.append("<item name=\"city\">" + city + "</item>");
		output.append("<item name=\"state\">" + state + "</item>");
		output.append("<item name=\"zipCode\">" + zipCode + "</item>");
		output.append("</resources>");
		
		
		/*StringBuilder sb = new StringBuilder();
		for(int i = 0 ; i< output.length(); i++){
			char c;
			if((c = output.charAt(i))==' '){
				sb.append("%20");
			}else{
				sb.append(c);
			}
		}
				*/
		return output.toString();
	}

	public void fromXML(InputSource xml) {
		try {
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp;
			sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();
			MyHandler handler = new MyHandler();
			xr.setContentHandler(handler);
			xr.setErrorHandler(handler);
			xr.parse(xml);
		} catch (SAXException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			e.printStackTrace();
		}
	}

	class MyHandler extends DefaultHandler {
		String lastElement;
		boolean begin;

		public void endElement(String uri, String localName, String qName) {
			begin=false;
		}

		public void startElement(String uri, String localName, String qName,
				Attributes attributes) {
			begin=true;
			for (int i = 0; i < attributes.getLength(); i++) {
				lastElement = attributes.getValue(i);
			}
		}

		public void characters(char[] ch, int start, int length) {
			String s = new String(ch, start, length);
			if(lastElement==null)return;
			if(!begin)return;
			if (lastElement.compareTo("IM1Server")==0) {
				if(s.compareTo("null")!=0){
					Contact.this.IM1Server = Integer.parseInt(s);
				}
			} else if (lastElement.compareTo("IM2Server")==0) {
				if(s.compareTo("null")!=0){
					Contact.this.IM2Server = Integer.parseInt(s);
				}
			} else if (lastElement.compareTo("priEmail")==0) {
				Contact.this.priEmail = s;
			} else if (lastElement.compareTo("secEmail1")==0) {
				Contact.this.secEmail1 = s;
			}else if (lastElement.compareTo("secEmail2")==0) {
				Contact.this.secEmail2 = s;
			}else if (lastElement.compareTo("secEmail3")==0) {
				Contact.this.secEmail3 = s;
			}else if (lastElement.compareTo("IM1user")==0) {
				Contact.this.IM1user = s;
			}else if (lastElement.compareTo("IM2user")==0) {
				Contact.this.IM2user = s;
			}else if (lastElement.compareTo("homePhone")==0) {
				Contact.this.homePhone = s;
			}else if (lastElement.compareTo("cellphone")==0) {
				Contact.this.cellphone = s;
			}else if (lastElement.compareTo("addr1")==0) {
				Contact.this.addr1 = s;
			}else if (lastElement.compareTo("addr2")==0) {
				Contact.this.addr2 = s;
			}else if (lastElement.compareTo("city")==0) {
				Contact.this.city = s;
			}else if (lastElement.compareTo("state")==0) {
				Contact.this.state = s;
			}else if (lastElement.compareTo("zipCode")==0) {
				Contact.this.zipCode = s;
			}
			
		}
	}

}
